#include <xitemfactory.h>

XItemFactory& XItemFactory::getInstance() {
    static XItemFactory factory;
    return factory;
}

void XItemFactory::addCreator(const std::wstring& key, Creator creator) {
    creators->insert_or_assign(key, creator);
}

std::shared_ptr<XItem> XItemFactory::create(const std::wstring& key) {
    auto itr = creators->find(key);
    if (itr != creators->end())
        return itr->second();
    return nullptr;
}

std::shared_ptr<XItemCall> XItemFactory::createCall(const std::wstring& key) {
    auto item = create(key);
    if (item)
        return item->asCall();
    return nullptr;
}

std::vector<std::wstring> XItemFactory::getKeys() {
    std::vector<std::wstring> output;
    auto itr = creators->begin();
    while (itr != creators->end()) {
        output.push_back(itr->first);
        itr ++;
    }

    return output;
}

XItemFactory::XItemFactory() {
    creators = std::make_unique<std::map<std::wstring, Creator>>();
}
